<?php
/***************************************************************************
 * 
 * Copyright (c) 2011 , Inc. All Rights Reserved
 * $Id$:DbTest.php,2011/07/01 11:07:59 
 * 
 **************************************************************************/
 
 
 
/**
 * @file DbTest.php
 * @author huqingping
 * @date 2011/07/01 11:07:59
 * @version 1.0 
 * @brief 
 *  
 **/
require_once dirname(__FILE__).'/../Db.php';
$conf = array(
	'gdp_svr'=>array(
		'read_timeout'=>1000,
		'write_timeout'=>1000,
		'connect_timeout'=>1000,
		'servers'=>array(
			array('ip'=>'10.32.12.169','port'=>11000),
		)   
	),
	'text_db'=>'text',
	'text_table'=>'t_text',
	'text_compress_len'=>20,
	'max_text_len'=>500,
	'default_db'=>'youa',
	'splits'=>array('t_text'=>array('text_id',array(DbContext::MOD_SPLIT=>100))),
	'log_func'=>'logsql',
	'test_mode'=>1
);
Db::init($conf);

final class Item extends Table
{
	protected $dbname = 'itemdb';
	protected $table = 'b_item';
	protected $properties = array('id','data','vr_desc','va_setting','mtime');
	protected $uniqueId = 'id';
	protected $keyProperty = 'id';
	protected $tbSpProperty = 'id';
	protected $tbSpMethod = array(DbContext::DIV_SPLIT=>2);
}

function logsql($sql)
{
	static $count = 0;
	if ($count == 0) {
		file_put_contents('sql.log','');
	}
	file_put_contents('sql.log',$sql."\r\n",FILE_APPEND);
	$count++;

}


function GuidTest()
{
	$id = Db::newGuid('name1');
	$id10 = Db::newGuid('name2',10);
}
function ForceTest()
{
	$db = Db::get('text');
	$db->forceUse(true);
	$db->forceMaster(true);
	$db->queryBySql('select * from t');
	$db->forceMaster(false);
	$db->queryBySql('select * from t');
	$db->forceUse(false);
	$db->queryBySql('select * from t');
}
function InsertTest()
{
	$db = Db::get('insert');
	$db->table('t_insert')->saveBody(array(
		'id'=>12321,
		'data'=>'aaaaaaaaaa',
		'vt_tt'=>'aa',
		'vr_cc'=>'213123dsfds2112312',
		'va_bb'=>array('a'=>'teswt')
	))->unique('xid')->time('atime','btime')->insert();
	$item = new Item();
	$item->data = 'aaaa';
	$item->vr_desc = "asldanjk2104ekjfdhjdsdfa";
	$item->va_setting = array("set"=>"data");
	$item->insert(array('mtime'=>1));
}
function UpdateTest()
{
	$db = Db::get('update');
	$db->table('t_update')->saveBody(array(
		'data'=>'aaaaaaaaaa',
		'vr_cc'=>'213123dsfds2112312',
		'va_bb'=>array('a'=>'teswt')
	))->where(array('id'=>2,'id2'=>array(1,3)))->in('id3',array(1,4))->limit(0,10)->update();
	$db->table('t_update')->saveBody(array(
		'data'=>'aaaaaaaaaa',
		'vr_cc'=>'213123dsfds2112312',
		'va_bb'=>array('a'=>'teswt')
	))->where('id>5 and id>0')->in('id3',array(1,4))->limit(0,10)->update();
	$item = new Item();
	$item->id = 1;
	$item->data = 'aaaa';
	$item->vr_desc = "asldanjk2104ekjfdhjdsdfa";
	$item->va_setting = array("set"=>"data");
	$item->update(array('mtime'=>1));
}

function GetTest()
{
	$db = Db::get('getdb');
	$db->table('gettable')->field('f1','f2')->order('id',false)->limit(0,20)->get(true);
	$db->table('gettable')->field('f1','f2')->where('id > 20')->order('id',false)->get(true);
	$db->table('gettable')->where(array('id'=>array(20,null),'status'=>2))->between('id3',1,3)->get();
	$db->table('gettable')->between('id2',20,30)->like('name','v2')->get();

	$item = new Item();
	$item->id = 200;
	$item->data = 'aaa';
	$db->oGet($item);
}
function InsertIgnoreTest()
{
	$db = Db::get('insertignore');
	$db->table('t_insertignore')->saveBody(array(
		'id'=>12321,
		'data'=>'aaaaaaaaaa',
		'vt_tt'=>'aa',
		'vr_cc'=>'213123dsfds2112312',
		'va_bb'=>array('a'=>'teswt')
	))->unique('xid')->time('atime','btime')->insertIgnore();
	$item = new Item();
	$item->data = 'aaaa';
	$item->vr_desc = "asldanjk2104ekjfdhjdsdfa";
	$item->va_setting = array("set"=>"data");
	$item->insertIgnore(array('mtime'=>1));
}

function InsertOrUpdateTest()
{
	$db = Db::get('insertorupdate');
	$db->table('t_insertorupdate')->saveBody(array(
		'id'=>12321,
		'data'=>'aaaaaaaaaa',
		'vt_tt'=>'aa',
		'vr_cc'=>'213123dsfds2112312',
		'va_bb'=>array('a'=>'teswt')
	))->unique('xid')->time('atime','btime')->insertIgnore();
	$item = new Item();
	$item->data = 'aaaa';
	$item->vr_desc = "asldanjk2104ekjfdhjdsdfa";
	$item->va_setting = array("set"=>"data");
	$item->insertIgnore(array('mtime'=>1));
}
function DeleteTest()
{
	$db = Db::get('deletedb');
	$db->table('deltable')->field('f1','f2')->order('id',false)->limit(0,20)->delete();
	$db->table('deltable')->field('f1','f2')->where('id > 20')->order('id',false)->delete();
	$db->table('deltable')->where(array('id'=>array(20,null),'status'=>2))->delete();

	$item = new Item();
	$item->id = 200;
	$item->data = 'aaa';
	$db->oDelete($item);
}
function querySqlTest()
{
	$db = Db::get('querydb');
	$db->queryBySql('select1');
	$db->queryBySql('select1 ? ?','arg1',2);
	$db->queryBySql(array(array('select1 ? ?','arg1',2)));
	$db->queryBySql(array(
		array('select1 ? ?','arg1',2),
		array('select2'),
		array('select ? ? ? ? ?',1,2,3,4,5),
	));
}
function commitTextTest()
{
	Db::commitText('');
	Db::commitText('aaaaa');
	Db::commitText('aaaaaaaaaaaaaaaaaaaaaaaaaaa');
	Db::commitText(array('aaaaaaa','bbbbbbbbbbbbbbbbbbbbbbbbbbddd'));
	Db::commitText(array('','bbbbbbbbbbbbbbbbbbddd'));
}
function quertTextTest()
{
	Db::queryText(1);
	Db::queryText(array(1));
	Db::queryText(array(1,2,101,202));
}
function getCountTest()
{
	$db = Db::get('countdb');
	$db->table('counttable')->where('a=1 and b<1')->in('f',array(1,2,3))->getCount();
}
function getExistTest()
{
	$db = Db::get('existdb');
	$db->table('existtable')->where('a=1 and b<1')->in('f',array(1,2,3))->getExist();
}

function transTest()
{
	$db = Db::get('transdb');
	$db->beginTx();
	$db->table('table1')->where('a>1')->getForUpdate();
	$db->table('table1')->where('a>1')->saveBody(array('a'=>array('exp'=>'b')))->update();
	$db->commit();

	$db->beginTx();
	$db->table('table2')->where('a>1')->getForUpdate();
	$db->table('table2')->where('a>1')->saveBody(array('a'=>array('exp'=>'b')))->update();
	$db->rollback();

	TxScope::beginTx();
	$db = Db::get('transdb');
	$db->table('table1')->where('a>1')->getForUpdate();
	$db->table('table1')->where('a>1')->saveBody(array('a'=>array('exp'=>'b')))->update();
	TxScope::commit();

	TxScope::beginTx();
	$db = Db::get('transdb');
	$db->table('table1')->where('a>1')->getForUpdate();
	$db->table('table1')->where('a>1')->saveBody(array('a'=>array('exp'=>'b')))->update();
	TxScope::rollback();
}

function followTest()
{
	$db = Db::get('followdb');
	$db->table('table1')->saveBody(array('key'=>'v'))->autoInc('kid')->insert();
	$db->table('table1')->saveBody(array('key'=>'v'))->follow('select shutup')->insert();

}

function runAllTest()
{
	logsql('--------------------guid------------------');
	GuidTest();
	logsql('--------------------force------------------');
	ForceTest();
	logsql('--------------------insert------------------');
	InsertTest();
	logsql('--------------------update------------------');
	UpdateTest();
	logsql('--------------------insertIgnore------------------');
	InsertIgnoreTest();
	logsql('--------------------insertOrUpdate------------------');
	InsertOrUpdateTest();
	logsql('--------------------Get------------------');
	GetTest();
	logsql('--------------------delete------------------');
	DeleteTest();
	logsql('--------------------querySql------------------');
	querySqlTest();
	logsql('--------------------commitText------------------');
	commitTextTest();
	logsql('--------------------queryText------------------');
	quertTextTest();
	logsql('--------------------getCount------------------');
	getCountTest();
	logsql('--------------------getExist------------------');
	getExistTest();
	logsql('--------------------trans------------------');
	transTest();
	logsql('--------------------follow------------------');
	followTest();
}
runAllTest();






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
